﻿using Microsoft.EntityFrameworkCore;
using SmartProduction.Domain;
using SmartProduction.Domain.yjh;
using SmartProduction.ErrorCode;
using SmartProduction.Infarstructrue;
using SmartProduction.Infarstructrue.Interface;
using WorkflowCore.Interface;
using WorkflowCore.Models;

namespace SmartProduction.Write.API.Approve.Workflow
{
    public class MultiLevelApprovalWorkflow : IWorkflow<Approval>
    {
        public string Id => "MultiLevelApprovalWorkflow";

        public int Version => 1;

        public void Build(IWorkflowBuilder<Approval> builder)
        {
            builder.Saga(saga => saga.StartWith<WorkFlowApproval>()

               .Input(x => x.ModelId, data => data.ModelId)

              .Input(x => x.LeaderId, data => data.LeaderId)
                .Input(x => x.LeaderName, data => data.LeaderName)
                  .Input(x => x.AppResult, data => data.AppResult)
                    .Input(x => x.Reason, data => data.Reason)
                      .Input(x => x.CreateTime, data => DateTime.Now)
                      .Then<WorkFlowApproval2>()
                      .Input(x => x.ModelId, data => data.ModelId)
                      .Input(x => x.AppResult, data => data.AppResult)

            );
        }


        public class WorkFlowApproval : StepBody
        {

            private readonly DataDbContext db;

            public WorkFlowApproval(DataDbContext db)
            {
                this.db = db;
            }

            public int AppId { get; set; }


            public int ModelId { get; set; }

            public int LeaderId { get; set; }


            public string LeaderName { get; set; }


            public int AppResult { get; set; }

            public string? Reason { get; set; }


            public DateTime? CreateTime { get; set; }


            public override ExecutionResult Run(IStepExecutionContext context)
            {
                var model = new Approval()
                {
                    ModelId = ModelId,
                    LeaderId = LeaderId,
                    LeaderName = LeaderName,
                    AppResult = AppResult,
                    Reason = Reason,
                    CreateTime = CreateTime,
                };
                db.AddAsync(model);
                db.SaveChangesAsync();
                Console.WriteLine("初始化请求");
                return ExecutionResult.Next();
            }
        }
        public class WorkFlowApproval2 : StepBody
        {
            private readonly IBaseRepository<Workflows> WorkflowsRepository;
            private readonly DataDbContext dbContext;

            public WorkFlowApproval2(IBaseRepository<Workflows> workflowsRepository,DataDbContext dbContext)
            {
                WorkflowsRepository = workflowsRepository;
                this.dbContext = dbContext;
            }

            public int AppResult { get; set; }

            public int ModelId { get; set; }

            public override ExecutionResult Run(IStepExecutionContext context)
            {
                Console.WriteLine("审批状态");

                var o = WorkflowsRepository.GetAll().Result.FirstOrDefault(x => x.WorkflowsId == ModelId);

                if (o == null)
                {
                    return ExecutionResult.Outcome(context.Workflow.Status);//重新请求审批
                }

                if (AppResult == 1)
                {
                    o.IsStatus = true;
                }
                else
                {
                    o.IsStatus = false;
                }

                WorkflowsRepository.UpdateAsync(o);

                Approval pro = new Approval
                {
                    ModelId = o.WorkflowsId,
                    LeaderId = o.CreatePerson,
                    LeaderName = o.CreatePerson.ToString(),
                    AppResult = 1,
                     CreateTime=o.CreateTime,
                      Reason="通过"
                   
                };
                dbContext.AddAsync(pro);
                dbContext.SaveChangesAsync();

                Console.WriteLine($"结束审批流程{context.Workflow.Status}");
                return ExecutionResult.Outcome(context.Workflow.Status);
            }
        }
    }
}
